﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using Rison.BusinessFacade.Helper;
using Rison.Web.Common;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Facade.NRBZ;
using Rison.BusinessFacade.Entity.ZP;
using Rison.BusinessFacade.Facade.ZP;
using Rison.BusinessFacade.Entity.NRBZ;

namespace Rison.Web.NRBZ
{
    public class NrfbPage : System.Web.UI.UserControl
    {
        public virtual string departid
        {
            get { return string.IsNullOrEmpty(Request.QueryString["DepartID"]) ? "" : Request.QueryString["DepartID"]; }
        }

        public virtual string year
        {
            get { return string.IsNullOrEmpty(Request.QueryString["year"]) ? System.DateTime.Today.Year.ToString() : Request.QueryString["year"]; }
        }


        public virtual string userid
        {
            get { return CurrentLoginedUser.UserId; }
        }

        public string UploadPath
        {
            get { return PageGlobalParams.PageUrlBase2 + GlobalParams.Instance.AttachmentUploadUrl; }
        }

        public virtual string NrbzId
        {
            get 
            {                
                return ((HiddenField)this.Page.FindControl("HiddenID")).Value;               

            }
            set { ((HiddenField)this.Page.FindControl("HiddenID")).Value = value; }
        }

        public virtual void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../ZP/SelfExaminationView.aspx?DepartID=" + departid);
        }

        public virtual DataRow RetriveNrbz()
        {
            NrbzFacade facade = new NrbzFacade();
            DataRow dr = facade.GetInformation(departid, year);
            return dr;
        }

        public virtual void LoadInformation(DataRow dr)
        {
        }

        public virtual long SaveInformation()
        {
            return 0;
        }

        /// <summary>
        /// upload file 
        /// </summary>
        /// <param name="sFilePath"></param>
        /// <returns></returns>
        public List<SysAttachment> NewUpload(string sFilePath)
        {

            List<SysAttachment> attList = new List<SysAttachment>();
            
            SysAttachment att = new SysAttachment();
            string[] a = sFilePath.Split(',');
            for (int i = 0; i < a.Length; i++)
            {
                string[] b = a[i].Split(';');
                if (b.Length > 1)
                {
                    string tmpVirtualPath = HttpContext.Current.Server.MapPath(b[0].ToString());
                    FileInfo f = new System.IO.FileInfo(tmpVirtualPath);
                    att = new SysAttachment();
                    att.FileName = b[1].ToString();
                    att.FilePath = f.Name;
                    att.FileSize = f.Length.ToString();
                    att.Extension = f.Extension;
                    attList.Add(att);

                    string fileName = HttpContext.Current.Server.MapPath(GlobalParams.Instance.AttachmentUploadUrl) + f.Name;
                    if (!File.Exists(fileName))
                    {
                        f.MoveTo(fileName);
                    }
                }
            }
            return attList;

        }

        

    }
}
